#!/usr/bin/env python
# -*- coding: utf-8 -*-
# title       :
# author      : jarry

import weakref
try:
    from PySide2.QtGui import *
    from PySide2.QtCore import *
    from PySide2.QtWidgets import *
except:
    from PySide.QtCore import *
    from PySide.QtGui import *
import maya.cmds as cmds
import maya.OpenMayaUI as omui
try:
    from shiboken import wrapInstance
except:
    from shiboken2 import wrapInstance


def dockToAEWindow(dialog_class, control_name='myControl', label_name='myLabel'):
    try:
        cmds.deleteUI(control_name)
        # logger.info('removed workspace {}'.format(dialog_class.CONTROL_NAME))
    except:
        pass

    # building the workspace control with maya.cmds
    main_control = cmds.workspaceControl(control_name, tabToControl=["AttributeEditor", -1], initialWidth=400, minimumWidth=1,
                                         widthProperty='preferred', label=label_name, floating=True)

    # now lets get a C++ pointer to it using OpenMaya
    control_widget = omui.MQtUtil.findControl(control_name)
    # conver the C++ pointer to Qt object we can use
    control_wrap = wrapInstance(int(control_widget), QWidget)

    # control_wrap is the widget of the docking window and now we can start working with it:
    control_wrap.setAttribute(Qt.WA_DeleteOnClose)
    win = dialog_class(control_wrap)
    # log.debug(control_wrap.findChild(QWidget, win.objectName()))  # <-- This line!

    # after maya is ready we should restore the window since it may not be visible
    cmds.evalDeferred(lambda *args: cmds.workspaceControl(main_control, e=True, restore=True))

    # will return the class of the dock content.
    return win
